package baseFx.tests.database;

import baseFx.common.utils.ClassUtils;
import baseFx.database.ResetSpace;
import baseFx.database.Sql;
import baseFx.database.query.DbEntityQuery;
import baseFx.database.query.impl.MysqlQuery;
import baseFx.database.where.QueryWhere;
import org.junit.Test;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

public class EntityQueryTest {
    @Test
    public void 等于() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member eq() {
                Member member = new Member();
                member.id = 1;
                member.name = "zhangsan";
                member.age = 18;
                return member;
            }
        });
    }

    @Test
    public void 不等于() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        Member member = new Member();
        member.id = 1;
        member.name = "zhangsan";
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member neq() {
                return member;
            }
        });
    }

    @Test
    public void 大于() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member greater() {
                Member member = new Member();
                member.id = 1;
                member.name = "zhangsan";
                return member;
            }
        });
    }

    @Test
    public void 小于() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member less() {
                Member member = new Member();
                member.id = 1;
                member.name = "zhangsan";
                return member;
            }
        });
    }

    @Test
    public void 模糊匹配() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member like() {
                Member member = new Member();
                member.id = 1;
                member.name = "zhangsan";
                return member;
            }
        });
    }

    @Test
    public void IN() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public List<Member> in() {
                return Arrays.asList(
                        ClassUtils.newInstance(Member.class, i -> i.id = 1),
                        ClassUtils.newInstance(Member.class, i -> {
                            i.id = 2;
                            i.name = "zs";
                        }),
                        ClassUtils.newInstance(Member.class, i -> {
                            i.id = 3;
                            i.name = "lisi";
                        }));
            }
        });
    }

    @Test
    public void isNull() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member isNull() {
                return ClassUtils.newInstance(Member.class, i -> {
                    i.id = 0;
                    i.name = "";
                });
            }
        });
    }

    @Test
    public void isNotNull() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member isNotNull() {
                return ClassUtils.newInstance(Member.class, i -> i.age = 0);
            }
        });
    }

    @Test
    public void 多个条件() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public List<Member> in() {
                return Arrays.asList(
                        ClassUtils.newInstance(Member.class, i -> i.id = 1),
                        ClassUtils.newInstance(Member.class, i -> i.id = 2),
                        ClassUtils.newInstance(Member.class, i -> i.id = 3));
            }

            @Override
            public Member eq() {
                Member member = new Member();
                member.name = "lisi";
                return member;
            }
        });
    }

    @Test
    public void 排序() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Member asc() {
                return ClassUtils.newInstance(Member.class, i -> i.id = 0);
            }

            @Override
            public Member desc() {
                return ClassUtils.newInstance(Member.class, i -> i.name = "");
            }
        });
    }

    @Test
    public void 返回数据数量控制() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
            for (String name : e.param.args.keySet()) {
                System.out.println(name + "=" + e.param.args.get(name));
            }
        });
        DbEntityQuery query = new MysqlQuery(sql);
        query.gets(new QueryWhere<Member>(Member.class) {
            @Override
            public Integer skip() {
                return 10;
            }

            @Override
            public Integer take() {
                return 5;
            }
        });
    }

    @Test
    public void 重置表名() {
        Sql sql = new TestSql();
        sql.eventBefore().add((s, e) -> {
            System.out.println(e.param.command);
        });
        DbEntityQuery query = new MysqlQuery(sql);
        try (ResetSpace rs = query.resetTable(Model.class, "666666")) {
            query.gets(Model.class);
        }
    }

    @Resource(name = "model_{0}")
    private static class Model {
        public Integer id;
    }
}
